package com.nuanshui.heatedloan.credit.utils;

import com.nuanshui.heatedloan.credit.constant.BorrowType;
import com.nuanshui.heatedloan.credit.constant.CalculateLateFeeRequest;
import com.nuanshui.heatedloan.credit.constant.CalculateLoanAmtResponse;

import java.math.BigDecimal;

/**
 * Created by wangxw on 2017/7/21.
 */
public class CalculateLoanAmt {

    /***
     *
     * @param baseRateInteger   基本利息   日利率
     * @param interestRateAmtInteger   服务费  日利率
     * @param loanAmt   贷款天数
     * @param loanDay  贷款的利息
     * @return
     */
    public static CalculateLoanAmtResponse calculateLoanLightAmt(Integer baseRateInteger, Integer interestRateAmtInteger,
                                                                 Integer loanAmt,Integer loanDay){
        CalculateLoanAmtResponse calculateLoanAmtResponse=new CalculateLoanAmtResponse();
        BigDecimal baseRateBigDecimal=new BigDecimal(baseRateInteger.toString());  //基本利率的BigDecimal类型
        BigDecimal interestRateBigDecimal=new BigDecimal(interestRateAmtInteger.toString());  //总利率的BigDecimal类型
        BigDecimal allRateBigDecimal=baseRateBigDecimal.add(interestRateBigDecimal);
        BigDecimal allRateBigDecimalAmt=allRateBigDecimal.
                multiply(new BigDecimal(loanAmt.toString())).
                multiply(new BigDecimal(loanDay.toString())).
                divide(new BigDecimal("10000.0"),0,BigDecimal.ROUND_UP);            //基本利息
        BigDecimal interestAmt=interestRateBigDecimal.
                multiply(new BigDecimal(loanAmt.toString())).
                multiply(new BigDecimal(loanDay.toString())).
                divide(new BigDecimal("10000.0"),0,BigDecimal.ROUND_UP);           //服务费
        BigDecimal baseBigDecimalAmt=allRateBigDecimalAmt.subtract(interestAmt);
        //总利息的BigDecimal类型
        //服务费的BigDecimal类型
        BigDecimal loanAmtBigDecimal=new BigDecimal(loanAmt.toString());
        BigDecimal orderAmt=allRateBigDecimalAmt.add(loanAmtBigDecimal);
        calculateLoanAmtResponse.setOrderAmt(orderAmt.intValue());//本息一起的
        calculateLoanAmtResponse.setInterestAmt(interestAmt.intValue());  //服务费
        calculateLoanAmtResponse.setBaseAmt(baseBigDecimalAmt.intValue());  //基本利息
        return calculateLoanAmtResponse;
    }

    /***
     *
     * @param baseRateInteger   基本利率    日利率
     * @param interestRateAmtInteger  总利率 =基本利率+服务费利率   而且是月利率
     * @param loanAmt   贷款金额
     * @param loanDay   贷款天数
     * @return
     */
    public static CalculateLoanAmtResponse calculateLoanSingleAmt(Integer baseRateInteger,
                                                                  Integer interestRateAmtInteger, Integer loanAmt, Integer loanDay){
        CalculateLoanAmtResponse calculateLoanAmtResponse=new CalculateLoanAmtResponse();
        BigDecimal baseRateBigDecimal=new BigDecimal(baseRateInteger.toString());  //基本利率的BigDecimal类型
        BigDecimal interestRateBigDecimal=new BigDecimal(interestRateAmtInteger.toString());  //总利率的BigDecimal类型
        BigDecimal baseAmt=baseRateBigDecimal.
                multiply(new BigDecimal(loanAmt.toString())).
                multiply(new BigDecimal(loanDay.toString())).
                divide(new BigDecimal("10000.0"),0,BigDecimal.ROUND_UP);         //基本利息
        BigDecimal interestAmt=interestRateBigDecimal.                                //服务费
                multiply(new BigDecimal(loanAmt.toString())).
                multiply(new BigDecimal(loanDay.toString())).
                divide(new BigDecimal("300000.0"),0,BigDecimal.ROUND_UP);
        BigDecimal serviceAmt=interestAmt.subtract(baseAmt);                          //服务费的BigDecimal类型
        BigDecimal loanAmtBigDecimal=new BigDecimal(loanAmt.toString());
        BigDecimal orderAmt=interestAmt.add(loanAmtBigDecimal);
        calculateLoanAmtResponse.setOrderAmt(orderAmt.intValue());
        calculateLoanAmtResponse.setBaseAmt(baseAmt.intValue());
        calculateLoanAmtResponse.setInterestAmt(serviceAmt.intValue());
        return calculateLoanAmtResponse;
    }

    /***
     * 计算逾期费
     * @param calculateLateFeeRequest
     * @return
     */
    public static int calculateLateFee(CalculateLateFeeRequest calculateLateFeeRequest){
        //如果逾期天数小于等于开始天数，直接返回
        if(calculateLateFeeRequest.getLateDay()<=calculateLateFeeRequest.getBeginDay()){
            return 0;
        }
        //单期贷计算逾期利息
        if(calculateLateFeeRequest.getBorrowType()== BorrowType.SINGLELOAN){
            return calculateSingleLateFee(calculateLateFeeRequest);
        }
        //闪电贷计算逾期利息
        else if(calculateLateFeeRequest.getBorrowType()==BorrowType.LIGHTLOAN){
            return calculateLightLateFee(calculateLateFeeRequest);
        }
        return 1;
    }

    public static  int calculateSingleLateFee(CalculateLateFeeRequest calculateLateFeeRequest){
        int singleFeeRateSmall=calculateLateFeeRequest.getSingleFeeRateSmall();  //获得单期贷的逾期利息  乘以10000
        int singleFeeRateBig=calculateLateFeeRequest.getSingleFeeRateBig();
        int payAmt=calculateLateFeeRequest.getPayAmt();   //已经还的金额
        int remainAmt=calculateLateFeeRequest.getLoanAmt()-calculateLateFeeRequest.getPayAmt();   //剩余需要还的
        int lateDay=calculateLateFeeRequest.getLateDay(); //逾期天数
        int beginDay=calculateLateFeeRequest.getBeginDay();  //开始时间
        BigDecimal baseRateBigDecimal=new BigDecimal(remainAmt+"");
        BigDecimal lateAmt=null;
        //小于15
        if(lateDay<=15){
            lateAmt=baseRateBigDecimal.
                    multiply(new BigDecimal(lateDay)).
                    multiply(new BigDecimal(singleFeeRateSmall)).
                    divide(new BigDecimal("10000.0"),0,BigDecimal.ROUND_UP).
                    add(new BigDecimal(1000));  //分计算
        }
        //15到120天
        else if(lateDay>15&&lateDay<=120){
            BigDecimal before= new BigDecimal(15).
                    multiply(new BigDecimal(singleFeeRateSmall));    //前15天的
            BigDecimal after=new BigDecimal(lateDay-15).        //15天之后的
                    multiply(new BigDecimal(singleFeeRateBig));
            BigDecimal all=before.add(after);
            lateAmt=baseRateBigDecimal.multiply(all).               //总费用
                    divide(new BigDecimal("10000.0"),0,BigDecimal.ROUND_UP).
                    add(new BigDecimal(1000));  //分计算
        }
        //大于120天
        else if(lateDay>120){
            BigDecimal before= new BigDecimal(15).
                    multiply(new BigDecimal(singleFeeRateSmall));    //前15天的
            BigDecimal after=new BigDecimal(105).        //15天之后的
                    multiply(new BigDecimal(singleFeeRateBig));
            BigDecimal all=before.add(after);
            lateAmt=baseRateBigDecimal.multiply(all).               //总费用
                    divide(new BigDecimal("10000.0"),0,BigDecimal.ROUND_UP).
                    add(new BigDecimal(1000));  //分计算
        }
        return lateAmt.intValue();
    }

    private static int calculateLightLateFee(CalculateLateFeeRequest calculateLateFeeRequest){
        int lightnFeeRate=calculateLateFeeRequest.getLightFeeRate();  //获得闪电贷的利息
        int payAmt=calculateLateFeeRequest.getPayAmt();   //已经还的金额
        int remainAmt=calculateLateFeeRequest.getLoanAmt()-calculateLateFeeRequest.getPayAmt();   //剩余需要还的
        int lateDay=calculateLateFeeRequest.getLateDay(); //逾期天数
        int beginDay=calculateLateFeeRequest.getBeginDay();  //开始时间
        BigDecimal baseRateBigDecimal=new BigDecimal(remainAmt+"");
        BigDecimal lateAmt=null;
        if(lateDay>120){
            lateDay=120;
        }
        lateAmt=baseRateBigDecimal.
                multiply(new BigDecimal(lateDay)).
                multiply(new BigDecimal(lightnFeeRate)).
                divide(new BigDecimal("10000.0"),0,BigDecimal.ROUND_UP).
                add(new BigDecimal(1000));  //分计算
        return lateAmt.intValue();
    }
}
